﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetroFramework.Forms;
using System.IO;
using ComLib.ComType;
using LogLib;
using System.Net;

namespace LocalFileCopying
{
    public partial class FormCopyingOption : MetroForm
    {
        private CopyingOption _option = new CopyingOption();
        public CopyingOption Option
        {
            get { return _option; }
        }

        private void LoadSettings()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            else
            {
                _option.Load();
            }
        }

        public FormCopyingOption()
        {
            LoadSettings();
            InitializeComponent();
        }

        private bool CheckInputting()
        {
            // 检查填写是否正确
            if (string.IsNullOrEmpty(textBoxFolder.Text))
            {
                MessageBox.Show("IP地址为空.");
                return false;
            }
       
            if (!Directory.Exists(textBoxFolder.Text))
            {
                MessageBox.Show("指定的目录不存在，请检查并重新输入正确的目录地址");
                return false;
            }
            return true;
        }

        private void InitCtrls()
        {           
            textBoxFolder.Text = _option.DestDirectory;          
            metroComboBoxDestFileType.SelectedIndex = (int)_option.CopyOption;
            metroComboBoxDateType.SelectedIndex = (int)_option.DirFormat;          
        }

        private void UpdateData()
        {
            _option.DestDirectory = textBoxFolder.Text;
            _option.DirFormat = (DirectoryFormat)metroComboBoxDateType.SelectedIndex;         
            _option.CopyOption = (CopyOptions)metroComboBoxDestFileType.SelectedIndex;
            _option.Save();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (!CheckInputting())
            {
                return;
            }
            UpdateData();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void FormFTPOption_Load(object sender, EventArgs e)
        {
            InitCtrls();
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog form = new FolderBrowserDialog();
            form.SelectedPath = "D:\\";
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBoxFolder.Text = form.SelectedPath;
            }
        }
    }
}
